import {reqcoupon} from '../../request/api'
//状态
const initState ={
    coupons:[],
}

//actionType
const TYPES={
    COUPON_CHANGE_COUPONS:"COUPON_CHANGE_COUPONS"
}

//修改state reducer
const reducer = (state=initState,action)=>{
    switch(action.type){
        case TYPES.COUPON_CHANGE_COUPONS:
            return {
                ...state,
                coupons:action.coupons
            }
        default:
            return state;
    }
}

//逻辑和异步actions actionCreators
export const actions={
    changecou:coupons=>({type:TYPES.COUPON_CHANGE_COUPONS,coupons}),

    requestcoupons:()=>{
        return (dispatch,getState)=>{
            let coupons = getCou(getState())
            if(coupons.length > 0){
                return
            }
            reqcoupon().then(d=>{
                dispatch(actions.changecou(d.data.list))
            })
        }
    }
    
}

//导出数据 Selectors
export const getCou = (state) =>state.Coupon.coupons

//导出reduer
export default reducer;